package com.person.servlet;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.person.daoImpl.AdminDaoImpl;
import com.person.pojo.ArticleNumInfo;
import com.person.vo.Article;

/**
 * Servlet implementation class ArticlePadingServlet
 */
//@WebServlet("/ArticlePadingServlet")
public class ArticlePagingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	public static boolean isDigit(String str) {
		boolean flag = true;
		
		if(str.length() == 0) {
			flag = false;
		}else {
			for(int i = 0; i < str.length(); i++) {
				if(str.charAt(i) < '0' || str.charAt(i) > '9') {
					flag = false;
					break;
				}
			}
		}
		return flag;
	}
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticlePagingServlet() {}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 * 分页与搜索合并处理
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		//获取当前管理员的id
		int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
		AdminDaoImpl adi =  new AdminDaoImpl();
		String title = (String)request.getParameter("search"); //搜索的标题内容
		if(title == null) {
			title = "";
		}
		
		String pageNumStr = request.getParameter("pageNum");
		int pageNum = 1;

		int pageSize = 5;	//每页最大记录数
		int artiSum = adi.articleFzzySum(title, userId);//获取当前文章的总数
		int pageSum = 0;	//文章的总也数
		//计算文章的总页数
		if(artiSum % pageSize != 0) {
			pageSum = artiSum / pageSize + 1;
		}else {
			pageSum = artiSum / pageSize;
		}
		
		//当前页码数的处理
		if(pageNumStr != null && isDigit(pageNumStr)) {
			pageNum = Integer.parseInt(pageNumStr);//当前页码数
			if(pageNum <= 0) {
				pageNum = 1;
			}
			if(pageNum > pageSum) {
				pageNum = pageSum;
			}
		}	
		
		//获取模糊查询到的文章列表
		List<Article> lists = adi.articlePagingAndFzzyQuery(pageNum, pageSize, title, userId);
		
		//封装数据
		ArticleNumInfo anif = new ArticleNumInfo();
		anif.setArtiSum(artiSum);
		anif.setPageSize(pageSize);
		anif.setPageSum(pageSum);
		anif.setLists(lists);
		anif.setPageNum(pageNum);
		
		RequestDispatcher test= request.getRequestDispatcher("/backManaSyst/articleList.jsp");
 		
		request.setAttribute("anif", anif);
 		
		test.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
